package com.hzya.frame.web.aop;

import com.hzya.frame.system.dictionaryshop.entity.DictionaryShopEntity;
import com.hzya.frame.system.dictionaryshop.service.IDictionaryShopService;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 拦截DicitonaryDao中的save*和update方法，并更新数据字典
 *
 * @author Administrator
 */
@Aspect
public class DictionaryCacheUpdateAop {
    @Autowired
    private IDictionaryShopService dictionaryPlatformService;

    private String checkMethodName;

    @Pointcut("execution(* com.hzya.frame..IDictionaryPlatformDao+.save*(..)) || execution(* com.hzya.frame..IDictionaryPlatformDao+.update*(..))")
    public void updateDictionaryCache() {
    }

    ;

    @AfterReturning(pointcut = "updateDictionaryCache()",  returning = "returnObj")
    public boolean runAfterReturning(DictionaryShopEntity returnObj) throws Throwable {

        Map<String, DictionaryShopEntity> cacheDictionaryMap = dictionaryPlatformService.createDictionaryCacheMap();
        String key = returnObj.getTab_name() + "#" + returnObj.getColumn_name() + "#" + returnObj.getColumn_value();
        cacheDictionaryMap.remove(key);
        cacheDictionaryMap.put(key, returnObj);
        return false;
    }

    public String getCheckMethodName() {
        return checkMethodName;
    }

    public void setCheckMethodName(String checkMethodName) {
        this.checkMethodName = checkMethodName;
    }

}
